var path = require('path')
var webpack = require('webpack')

//进度条插件
var NyanProgressPlugin = require('nyan-progress-webpack-plugin')

//主要是为了抽离css样式，防止将样式打包在js中引起页面样式加载错乱的现象
ExtractTextPlugin = require('extract-text-webpack-plugin')

module.exports = {
    entry: './src/main.js',
    output: {
        path: path.resolve(__dirname, './dist'),
        publicPath: '/dist/',
        filename: 'build.js'
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                use: [
                    'css-loader'
                ],
            },
            {
                test: /\.scss$/,
                use: process.env.NODE_env !== 'production' ? [
                    'vue-style-loader',
                    'css-loader',
                    'sass-loader'
                ] : ExtractTextPlugin.extract('css-loader!sass-loader')
            },
            {
                test: /\.vue$/,
                loader: 'vue-loader',
                options: {
                    loaders: {
                        'scss': [
                            'vue-style-loader',
                            'css-loader',
                            'sass-loader'
                        ],
                        'sass': [
                            'vue-style-loader',
                            'css-loader',
                            'sass-loader?indentedSyntax'
                        ]
                    }
                    // other vue-loader options go here
                }
            },
            {
                test: /\.js$/,
                loader: 'babel-loader',
                exclude: /node_modules/
            },
            {
                test: /\.(png|jpg|gif|svg|eot|woff|woff2|ttf)$/,
                loader: 'file-loader',
                options: {
                    name: '[name].[ext]?[hash]'
                }
            },
            { test: /\.(eot|woff|ttf)$/, loader: 'file-loader' }
        ]
    },
    resolve: {
        alias: {
            'vue$' : 'vue/dist/vue.esm.js',
            '@' : path.resolve(__dirname, './src')
        },
        extensions: ['*', '.js', '.vue', '.json']
    },

    //webpack-dev-server 配置
    devServer: {
        // contentBase:"./" 本地服务器在哪个目录搭建页面，
        historyApiFallback: true, // 搭建SPA应用非常有用，使用的是HTML5 History Api , 任意的跳转或者404响应都可以指向Index.html
        noInfo: true, // only errors & warns on hot reload
        overlay: true // build error
    },

    //webpack性能提示
    performance: {
        hints: false
    },

    //https://juejin.im/post/58293502a0bb9f005767ba2f
    // 作用是可以debug生产环境下压缩后的代码
    devtool: '#eval-source-map', // 每个module会通过eval()来执行，并且生成一个Data-url形式的SourceMap
    plugins: [
        new NyanProgressPlugin()
    ]
}

if (process.env.NODE_env === 'production') {
    module.exports.entry = './src/lib/index.js'
    module.exports.output = {
        path: path.resolve(__dirname, './dist'),
        publicPath:'/dist/',
        filename:'index.js',

        //开发一个库，这些库既可以用commonjs和amd的方式使用，也可以通过script标签的方式引入使用，目前很多库都是支持这几种使用方式的。
        library: 'VueNotification', // require时候的模块名
        libraryTarget: 'umd', // umd的方式引入
        umdNameDefine: true
    }
    module.exports.devtool = '#source-map'
    module.exports.plugins = (module.exports.plugins || []).concat([
        new webpack.DefinePlugin({
            'process.env' : {
                NODE_ENV: '"production"'
            }
        }),
        new webpack.optimize.UglifyJsPlugin({
            sourceMap: true,
            compress: {
                warnings: false
            }
        }),
        new webpack.LoaderOptionsPlugin({
            minimize: true
        }),
        new ExtractTextPlugin({
            filename: 'style.css',
            allChunks: true
        })
    ])
}
